import { http } from "@/utils/http";
import { lmsUrlApi } from "../utils";

export type Result = {
  list: Array<any>;
  total?: number;
};
type TreeNode = {
  name: string;
  type: number;
  parentId: number;
  sort: number;
  leaderUserId: number;
  phone: string;
  email: string;
  status: number;
  id: number;
  createTime: number;
  remark: string;
};
export const transitionTreeData = (r: Array<any>) => {
  const li: TreeNode[] = [];
  r.forEach(item => {
    li.push({
      name: item.name,
      type: 1,
      parentId: item.parentId,
      sort: 0,
      leaderUserId: 1,
      phone: "",
      email: "",
      status: 1,
      id: item.folderId,
      createTime: item.createdAt,
      remark: ""
    });
  });
  return li;
};

export type AddCourseFolder = {
  name: string;
  parentId: string;
  describe: string;
};

export type CourseFolder = {
  folderId: string;
  name: string;
  describe: string;
  parentId: string;
  sort: number;
};

export type AddCourse = {
  name: string;
  code: string;
  cover: string;
  describe: string;
  allowedX: number;
  classHour: number;
  classScore: number;
  label: string;
  supportMobile: boolean;
  folderId: string;
  sort: number;
  coursewareIds: string[];
};

export type SearchCourse = {
  name: string;
  folderId: string;
  pageNum: number;
  pageSize: number;
};

// 获取课程目录列表
export const getCourseFolderList = () => {
  return http.request<CourseFolder[]>("get", lmsUrlApi("course/folderList"));
};
// 创建课程目录
export const createFolder = (data: AddCourseFolder) => {
  return http.request("post", lmsUrlApi("course/createFolder"), { data });
};
// 删除课课程目录
export const deleteFolder = (id: string) => {
  return http.request("delete", lmsUrlApi("course/courseFolder/" + id));
};

// 创建课程
export const createCourse = (data: AddCourse) => {
  return http.request<any>("post", lmsUrlApi("course/create"), { data });
};

export type bindCoursewareDto = {
  courseId: string;
  coursewareIds: string[];
};
//绑定课件
export const bindCourseware = (data: bindCoursewareDto) => {
  return http.request("post", lmsUrlApi("course/bindCourseware"), { data });
};

export type publishCourseDto = {
  courseId: string;
  isPublic: boolean;
  userIds: string[];
  orgIds: string[];
};
//发布课程
export const publishCourse = (data: publishCourseDto) => {
  return http.request("post", lmsUrlApi("course/publish"), { data });
};
//获取课程列表，前端分页
export const getCoursePage = (params: SearchCourse) => {
  return http.request<Result>("get", lmsUrlApi("course/list"), { params });
};
//上传课程封面
export const fileUploadUrl = () => {
  const formData = new FormData();
  // @ts-ignore
  formData.append("file", option.file);
  return http
    .post(
      lmsUrlApi("courseware/upload"),
      { data: formData },
      {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      }
    )
    .then(res => {
      return res;
    });
};
// 删除课程
export const deleteCourse = (id: string) => {
  return http.request("delete", lmsUrlApi("course/" + id));
};

// 将部门数据格式化为树形结构
export const simpleTransitionTree = data => {
  const cloneData = JSON.parse(JSON.stringify(data));
  return cloneData.filter(father => {
    const branchArr = cloneData.filter(
      child => father["deptId"] == child["parentId"]
    );
    branchArr.length > 0 ? (father["children"] = branchArr) : "";
    return father["parentId"] == "0";
  });
};
